<template>
  <div>
    <!-- 资产负债表查询 -->
    <TopFilter ref="form" :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhBalanceSheetQuery"
        :isToperInfo="false"
        :isColumnFilter="false"
        :isSelectColumn="false"
        :columns="columns"
        :dataSource="checkList"
        :onColumnsChange="columns => (this.columns = columns)"
      >
      </FilterTable>

    </Spin>

    <!-- 经销商查询 -->
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
        <!-- 详情展示 -->
    <Drawer :visible.sync="actionsOwner.visible" :title="actionsOwner.title" width="80%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DetailView :formData="this.tableList" :viewStatus="this.passStatus" :requestId="this.reportId" :year="this.year" :month="this.month" @close="closeSetting" @refrish="refrishList"/>
    </Drawer>
  </div>
</template>
<script>
import moment from 'moment';
import { getOwnerCode } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { queryList, exportExcel, submitData } from '@/api/omFinance/CRReportManage/BalanceSheetQuery';
import OwnerSearch from '../OwnerSearch';
import DetailView from './detailView';

export default {
  name: 'BalanceSheetQuery',
  mixins: [dictionary],

  components: {
    OwnerSearch,
    DetailView
  },
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      formList: [],
      isDealer: false,
      isDisabled: true,
      exportLoading: false,
      formData: {},
      tableList: [],
      reportId: '',
      year: moment().format('YYYY'),
      month: moment().format('MM'),
      dealerCode: '',
      statusCode:'',
      checkList:[],
      actionsOwner: { visible: false, title: '', type: '' },
      passStatus:'',
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
     openHandler(data){
       this.passStatus = data.balanceStatus;
     this.reportId = data.id; // 导出传id
        this.tableList = [
            {
              column1: '编制单位:',
              column2: data.dealerCode,
              column3: data.dealerName,
              column4: '',
              column5: '',
              column6: '',
              column7: '',
              column8: '单位：元'
            },
            {
              column1: '资    产',
              column2: '行次',
              column3: '年初数',
              column4: '期末数',
              column5: '负债和股东权益',
              column6: '行次',
              column7: '年初数',
              column8: '期末数'
            },
            {
              column1: '流动资产：',
              column2: '',
              column3: '',
              column4: '',
              column5: '流动负债：',
              column6: '',
              column7: '',
              column8: ''
            },
            {
              column1: '货币资金',
              column2: '1',
              column3: data.nchbzj,
              column4: data.qmhbzj,
              column5: '短期借款',
              column6: '68',
              column7: data.ncdqjk,
              column8: data.qmdqjk
            },
            {
              column1: '短期投资',
              column2: '2',
              column3: data.ncnqtz,
              column4: data.qmnqtz,
              column5: '应付票据',
              column6: '69',
              column7: data.ncyfpj,
              column8: data.qmyfpj
            },
            {
              column1: '应收票据',
              column2: '3',
              column3: data.ncyspj,
              column4: data.qmyspj,
              column5: '应付账款',
              column6: '70',
              column7: data.ncyifzk,
              column8: data.qmyifzk
            },
            {
              column1: '应收股利',
              column2: '4',
              column3: data.ncysgl,
              column4: data.qmysgl,
              column5: '预收账款',
              column6: '71',
              column7: data.ncyuszk,
              column8: data.qmyuszk
            },
            {
              column1: '应收利息',
              column2: '5',
              column3: data.ncyslx,
              column4: data.qmyslx,
              column5: '应付职工薪酬',
              column6: '72',
              column7: data.ncyfzgxc,
              column8: data.qmyfzgxc
            },
            {
              column1: '应收账款',
              column2: '6',
              column3: data.ncyszk,
              column4: data.qmyszk,
              column5: '应付股利',
              column6: '74',
              column7: data.ncyfgl,
              column8: data.qmyfgl
            },
            {
              column1: '其他应收款',
              column2: '7',
              column3: data.ncqtysk,
              column4: data.qmqtysk,
              column5: '应交税金',
              column6: '75',
              column7: data.ncyjsj,
              column8: data.qmyjsj
            },
            {
              column1: '预付账款',
              column2: '8',
              column3: data.ncyfzk,
              column4: data.qmyfzk,
              column5: '其他应交款',
              column6: '80',
              column7: data.ncqtyjk,
              column8: data.qmqtyjk
            },
            {
              column1: '应收补贴款',
              column2: '9',
              column3: data.ncysbtk,
              column4: data.qmysbtk,
              column5: '其他应付款',
              column6: '81',
              column7: data.ncqtyfk,
              column8: data.qmqtyfk
            },
            {
              column1: '存  货',
              column2: '10',
              column3: data.ncch,
              column4: data.qmch,
              column5: '预提费用',
              column6: '82',
              column7: data.ncytfy,
              column8: data.qmytfy
            },
            {
              column1: '待摊费用',
              column2: '11',
              column3: data.ncdtfy,
              column4: data.qmdtfy,
              column5: '预计负债',
              column6: '83',
              column7: data.ncyjfz,
              column8: data.qmyjfz
            },
            {
              column1: '一年内到期的长期债权投资',
              column2: '12',
              column3: data.ncynndqdcqzqtz,
              column4: data.qmynndqdcqzqtz,
              column5: '一年内到期的长期负债',
              column6: '86',
              column7: data.ncynndqdcqfz,
              column8: data.qmynndqdcqfz
            },
            {
              column1: '其他流动资产',
              column2: '13',
              column3: data.ncqtldzj,
              column4: data.qmqtldzj,
              column5: '其他流动负债',
              column6: '90',
              column7: data.ncqtldfz,
              column8: data.qmqtldfz
            },
            {
              column1: '流动资产合计',
              column2: '14=1+2+3+4+5+6+7+8+9+10+11+12+13',
              column3: data.ncldzchj,
              column4: data.qmldzchj,
              column5: '流动负债合计',
              column6: '91=68+69+70+71+72+74+75+80+81+82+83+86+90',
              column7: data.ncldfzhj,
              column8: data.qmldfzhj
            },
            {
              column1: '长期投资：',
              column2: '',
              column3: '',
              column4: '',
              column5: '长期负债：',
              column6: '',
              column7: '',
              column8: ''
            },
            {
              column1: '长期股权投资',
              column2: '15',
              column3: data.nccqgqtz,
              column4: data.qmcqgqtz,
              column5: '长期借款',
              column6: '101',
              column7: data.nccqjk,
              column8: data.qmcqjk
            },
            {
              column1: '长期债权投资',
              column2: '16',
              column3: data.nccqzqtz,
              column4: data.qmcqzqtz,
              column5: '应付债券',
              column6: '102',
              column7: data.ncyfzq,
              column8: data.qmyfzq
            },
            {
              column1: '长期投资合计',
              column2: '17=15+16',
              column3: data.nccqtzhj,
              column4: data.qmcqtzhj,
              column5: '长期应付款',
              column6: '103',
              column7: data.nccqyfk,
              column8: data.qmcqyfk
            },
            {
              column1: '固定资产：',
              column2: '',
              column3: '',
              column4: '',
              column5: '专项应付款',
              column6: '106',
              column7: data.nczxyfk,
              column8: data.qmzxyfk
            },
            {
              column1: '固定资产原价',
              column2: '39',
              column3: data.ncgdzcyj,
              column4: data.qmgdzcyj,
              column5: '其他长期负债',
              column6: '108',
              column7: data.ncqtcqfz,
              column8: data.qmqtcqfz
            },
            {
              column1: '减：累计折旧',
              column2: '40',
              column3: data.ncljzj,
              column4: data.qmljzj,
              column5: '长期负债合计',
              column6: '110=101+102+103+106+108',
              column7: data.nccqfzhj,
              column8: data.qmcqfzhj
            },
            {
              column1: '固定资产净值',
              column2: '41=39-40',
              column3: data.ncgdzcjz,
              column4: data.qmgdzcjz,
              column5: '递延税项：',
              column6: '',
              column7: '',
              column8: ''
            },
            {
              column1: '减：固定资产减值准备',
              column2: '42',
              column3: data.ncgdzcjzzb,
              column4: data.qmgdzcjzzb,
              column5: '递延税款贷项',
              column6: '111',
              column7: data.ncdyskdx,
              column8: data.qmdyskdx
            },
            {
              column1: '固定资产净额',
              column2: '43=41-42',
              column3: data.ncgdzcje,
              column4: data.qmgdzcje,
              column5: '负债合计',
              column6: '114=91+110+111',
              column7: data.ncfzhj,
              column8: data.qmfzhj
            },
            {
              column1: '工程物资',
              column2: '44',
              column3: data.ncgcwz,
              column4: data.qmgcwz,
              column5: '',
              column6: '',
              column7: '',
              column8: ''
            },
            {
              column1: '在建工程',
              column2: '45',
              column3: data.nczjgc,
              column4: data.qmzjgc,
              column5: '',
              column6: '',
              column7: '',
              column8: ''
            },
            {
              column1: '固定资产清理',
              column2: '46',
              column3: data.ncgdzcql,
              column4: data.qmgdzcql,
              column5: '股东权益',
              column6: '',
              column7: '',
              column8: ''
            },
            {
              column1: '固定资产合计',
              column2: '50=43+44+45+46',
              column3: data.ncgdzchj,
              column4: data.qmgdzchj,
              column5: '实收资本',
              column6: '115',
              column7: data.ncsszb,
              column8: data.qmsszb
            },
            {
              column1: '无形资产及其他资产：',
              column2: '',
              column3: '',
              column4: '',
              column5: '减：已归还投资',
              column6: '116',
              column7: data.ncyghtz,
              column8: data.qmyghtz
            },
            {
              column1: '无形资产',
              column2: '51',
              column3: data.ncwxzc,
              column4: data.qmwxzc,
              column5: '实收资本净额',
              column6: '117=115-116',
              column7: data.ncsszbje,
              column8: data.qmsszbje
            },
            {
              column1: '长期待摊费用',
              column2: '52',
              column3: data.nccqdtfy,
              column4: data.qmcqdtfy,
              column5: '资本公积',
              column6: '118',
              column7: data.nczbgj,
              column8: data.qmzbgj
            },
            {
              column1: '其他长期资产',
              column2: '53',
              column3: data.ncqtcqzc,
              column4: data.qmqtcqzc,
              column5: '盈余公积',
              column6: '119',
              column7: data.ncyygj,
              column8: data.qmyygj
            },
            {
              column1: '无形资产及其他资产合计',
              column2: '60=51+52+53',
              column3: data.ncwxzcjqtzchj,
              column4: data.qmwxzcjqtzchj,
              column5: '其中：法定公益金',
              column6: '120',
              column7: data.ncfdgyj,
              column8: data.qmfdgyj
            },
            {
              column1: '递延税项：',
              column2: '',
              column3: '',
              column4: '',
              column5: '未分配利润',
              column6: '121',
              column7: data.ncwfplr,
              column8: data.qmwfplr
            },
            {
              column1: '递延税款借项',
              column2: '61',
              column3: data.ncdyskjx,
              column4: data.qmdyskjx,
              column5: '所有者权益合计',
              column6: '122=117+118+119+121',
              column7: data.ncsyzqyhj,
              column8: data.qmsyzqyhj
            },
            {
              column1: '资产总计',
              column2: '67=14+17+50+60+61',
              column3: data.nczczj,
              column4: data.qmzczj,
              column5: '负债和所有者权益总计',
              column6: '135=114+122=67（校验）',
              column7: data.ncfzhsyzqyzj,
              column8: data.qmfzhsyzqyzj
            }
          ];
        
        this.actionsOwner = { visible: true, title: '编辑', type: 'show' };

    },
         //刷新
  refrishList(){
    this.getList(this.year, this.month, this.dealerCode,this.statusCode);
  },
    closeSetting() {
        this.actionsOwner.visible = false;
    },
    seacherFun() {
      this.isDealer = true;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(params) {
      this.year = params.yearMonth.split('-')[0];
      this.month = params.yearMonth.split('-')[1];
      this.dealerCode = params.ownerCode;
      this.statusCode = params.balanceStatus;
      console.log(params);
      this.getList(this.year, this.month, this.dealerCode,this.statusCode);
    },
    resetChange(params) {
      this.year = params.yearMonth.split('-')[0];
      this.month = params.yearMonth.split('-')[1];

      // this.getList(this.year, this.month, '');
      this.tableList = [];
    },
    // 导入
    importHandle() {
      this.isImport = true;
    },
    emitCloseHandler(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = dealerCodes.toString();
        this.topFilterList.find(x => x.fieldName == 'dealerName').initialValue = dealerNames.toString();
      }
    },
    // 表单数据
    formChangeHandle(val) {
      this.saveData = Object.assign({}, this.saveData, val);
      console.log(val, this.saveData, '---奥利给---');
    },
    // 驳回
    async dataHandle(status) {
      this.formList[0].rules[0].required = true;
      this.$refs.formPanel.SUBMIT_FORM();
      if (!this.formList[0].initialValue) return;
      this.saveData.id = this.reportId ? this.reportId : ''; // Id
      this.saveData.balanceStatus = status; // 状态

      const res = await submitData(this.saveData);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '操作成功' });
        this.getList(this.year, this.month, this.dealerCode);
      }
    },
    // 查询列表
    async getList(Year = '', Month = '', dealerCode = '',statusCode = '') {
      this.exportLoading = true;
      const res = await queryList({ reportYear: Year, reportMonth: Month, dealerCode: dealerCode ,balanceStatus:statusCode});
      this.reportId = '';
      this.tableList = [];
      if (res.resultCode == 200) {
        if (res.data) {
          this.isDisabled = false;
          const data = res.data; // 表格
          this.reportId = data.id; // 导出传id
          this.checkList = res.data;
        } else {
          this.isDisabled = true;
          this.tableList = [];
          // 驳回意见清空
          this.formList[0].rules[0].required = false;
          this.formList[0].initialValue = '';
        }
      }
      this.exportLoading = false;
    },
    // 导出
    async exportHandle() {
      await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLoading = true;
          let param = { id: this.reportId };
          exportExcel(param)
            .then(response => {
              exportExcelFile(response.data, '资产负债表查询');
              this.exportLoading = false;
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },

    createdFrom() {
      return [
        {
          type: 'DATE',
          label: '月度',
          fieldName: 'yearMonth',
          dateType: 'month',
          initialValue: moment().format('YYYY-MM'),
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请选择',
          rules: [{ required: false, message: '请选择', trigger: 'blur' }],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        // {
        //   type: 'INPUT',
        //   label: '经销商名称',
        //   fieldName: 'dealerName',
        //   placeholder: '',
        //   disabled: true
        // },
        {
          type: 'SELECT',
          label: '审核状态',
          fieldName: 'balanceStatus',
          placeholder: '',
          itemList: this.createDictList('6078')
        }
      ];
    },
    createdTable() {
      return  [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          render: props => {
            return (
              <div style="text-align:center">
                <el-button size="mini" type="text" onClick={() => this.openHandler(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
       {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报日期',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '状态',
          dataIndex: 'balanceStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('6078'),
          render: props => {
            return <span>{this.createDictText(props.row.balanceStatus, '6078')}</span>;
          }
        },
        {
          title: '驳回原因',
          dataIndex: 'checkDesc',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
